<?php
namespace app\home\controller;
use app\home\model\UserOrder;
use app\home\service\GameProcessMonthCard;
use app\home\service\NotifyService;
use Exception;
use think\Cache;
use think\Controller;
use think\Request;

class AdFacebook extends Controller
{
    //需要回传的渠道
    public  $channel_id = [800021,800023,800024,800027,800028,800029,800032,800033,800035,800036,800037];
    public static $channel_id_facebook = [
        800021=>'704165215221257',
        800023=>'951099073335596',
        800024=>'424524236681843',
        800027=>'946071803972028',
        800028=>'7376390789096558',
        800029=>'2305736809630451',
        800032=>'970567428057498',
        800033=>'461755486535092',
        800035=>'871723538097717',
        800036=>'366290936453566',
        800037=>'1149100322883700',
    ];

    public function ip(){
        header('Content-Type: text/html;charset=utf-8');
        header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
        header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
        header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 
        
        $params = Request::instance()->param();
        mylog($params);

        if(empty($params['fbc']) || empty($params['fbp'])){
            return '缺少参数';
        }
        $res = db('ad_facebook','db_operatelog')->where(['ip'=>Request()->ip()])->find();
        if(empty($res)){
            db('ad_facebook','db_operatelog')->insert([
                'fbc'=>$params['fbc'],
                'fbp'=>$params['fbp'],
                'ip'=>Request()->ip(),
                'date'=>date('Y-m-d H:i:'),
            ]);
        }else{
            db('ad_facebook','db_operatelog')->where(['ip'=>Request()->ip()])->update([
                'fbc'=>$params['fbc'],
                'fbp'=>$params['fbp'],
            ]);
        }
        
        exit('成功');
    }


    /**
     * 注册回传
     */
    public function register()
    {
        header('Content-Type: text/html;charset=utf-8');
        header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
        header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
        header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 
        try{
            mylog('AdFacebook_Lead');
            $params = Request::instance()->param();
            mylog($params);

            $res = db('ad_facebook','db_operatelog')->where(['ip'=>Request()->ip()])->find();
            if(empty($res)){
                return '没有ip匹配';
            }
            db('ad_facebook','db_operatelog')->where(['ip'=>Request()->ip()])->update(['user_id'=>$params['user_id']]);

            $facebook_id = self::$channel_id_facebook[$params['channel_id']];
            if(empty($facebook_id)){
                mylog('$facebook_id false');
                return '没有facebook_id匹配';
            }
            $url = 'https://graph.facebook.com/v16.0/'.$facebook_id.'/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';

            // if($params['channel_id'] == 800023){
            //     $url = 'https://graph.facebook.com/v16.0/951099073335596/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }elseif($params['channel_id'] ==800024){
            //     $url = 'https://graph.facebook.com/v16.0/424524236681843/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }elseif($params['channel_id'] ==800027){
            //     $url = 'https://graph.facebook.com/v16.0/946071803972028/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }elseif($params['channel_id'] ==800028){
            //     $url = 'https://graph.facebook.com/v16.0/7376390789096558/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }elseif($params['channel_id'] ==800029){
            //     $url = 'https://graph.facebook.com/v16.0/2305736809630451/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }elseif($params['channel_id'] ==800032){
            //     $url = 'https://graph.facebook.com/v16.0/970567428057498/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }elseif($params['channel_id'] ==800033){
            //     $url = 'https://graph.facebook.com/v16.0/461755486535092/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }elseif($params['channel_id'] ==800035){
            //     $url = 'https://graph.facebook.com/v16.0/871723538097717/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }elseif($params['channel_id'] ==800036){
            //     $url = 'https://graph.facebook.com/v16.0/366290936453566/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }elseif($params['channel_id'] ==800037){
            //     $url = 'https://graph.facebook.com/v16.0/1149100322883700/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }else{
            //     //兼容之前上线 没传渠道号
            //     $url = 'https://graph.facebook.com/v16.0/704165215221257/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }

            $data = [
                'data'=>[
                    [
                        'action_source'=>'website',
                        'event_name'=>'CompleteRegistration',
                        // 'event_time'=>strtotime('-8 hour'),
                        'event_time'=>time(),
                        'user_data'=>[
                            'fbc'=>$res['fbc'],
                            'fbp'=>$res['fbp']
                        ]
                    ]
                ]
            ];
            $result = self::request_by_curl($url, json_encode($data));
            mylog($result);
        }catch(\Exception $e){
            mylog($e->getMessage());
            return 'false';
        }
        return 'true';
    }

   
    //支付回传
    public function pay($user_id,$amount,$channel_id){
        try{
            $res = db('ad_facebook','db_operatelog')->where(['user_id'=>$user_id])->find();
            if(empty($res)){
                return;
            }
            mylog('AdFacebook_Purchase');

            $facebook_id = self::$channel_id_facebook[$channel_id];
            if(empty($facebook_id)){
                mylog('$facebook_id false');
                return '没有facebook_id匹配';
            }
            $url = 'https://graph.facebook.com/v16.0/'.$facebook_id.'/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';

            // if($channel_id == 800023){
            //     $url = 'https://graph.facebook.com/v16.0/951099073335596/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }
            // if($channel_id ==800024){
            //     $url = 'https://graph.facebook.com/v16.0/424524236681843/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }
            // if($channel_id == 800021){
            //     $url = 'https://graph.facebook.com/v16.0/704165215221257/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }
            // if($channel_id == 800027){
            //     $url = 'https://graph.facebook.com/v16.0/946071803972028/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }
            // if($channel_id == 800028){
            //     $url = 'https://graph.facebook.com/v16.0/7376390789096558/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }
            // if($channel_id == 800029){
            //     $url = 'https://graph.facebook.com/v16.0/2305736809630451/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }
            // if($channel_id == 800032){
            //     $url = 'https://graph.facebook.com/v16.0/970567428057498/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }
            // if($channel_id == 800033){
            //     $url = 'https://graph.facebook.com/v16.0/461755486535092/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }
            // if($channel_id == 800035){
            //     $url = 'https://graph.facebook.com/v16.0/871723538097717/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }
            // if($channel_id == 800036){
            //     $url = 'https://graph.facebook.com/v16.0/366290936453566/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }
            // if($channel_id == 800037){
            //     $url = 'https://graph.facebook.com/v16.0/1149100322883700/events?access_token=EAAJicfZCy1foBOwFh8YUTWOWJ4d3xE9CDW7N4wyVK48eWiRuCbk1madTWUoF0VAxTHgO6v4M0ewP60hC0gSqBKdCz3ZBG6nc51qPLp3ZCtDZCn4SB7RZBPFDM1Eg80pfHeyQvajLbo4elgNlU57OHayoW4GLBGbheJGvHeh5z2oul8WLWExPalMxZCb7pd3ehm8QZDZD';
            // }
            $data = [
                'data'=>[
                    [
                        'action_source'=>'website',
                        'event_name'=>'Purchase', 
                        // 'event_time'=>strtotime('-8 hour'),
                        'event_time'=>time(),
                        'user_data'=>[
                            'fbc'=>$res['fbc'],
                            'fbp'=>$res['fbp']
                        ],
                        'custom_data'=>[
                            'currency'=>'usd',
                            'value'=>round($amount/6,2)
                        ]
                    ]
                ]
            ];
            $result = self::request_by_curl($url, json_encode($data)); 
            mylog($result);
        }catch(\Exception $e){
            mylog($e->getMessage());
            return 'false';
        }
        return 'true';
    }
    public static function request_by_curl($rul, $data) {  
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL, $rul);
        curl_setopt($ch, CURLOPT_POST, 1); 
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $data = curl_exec($ch);
        curl_close($ch);      
        return $data;  
    }  
}